package com.nanhai.buddhist.college.module.study.controller.teacher.move;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.module.collegebase.api.collegeclass.CollegeClassApi;
import com.nanhai.buddhist.college.module.collegebase.api.collegeclass.dto.ClassesInfoDTO;
import com.nanhai.buddhist.college.module.collegebase.api.student.StudentApi;
import com.nanhai.buddhist.college.module.collegebase.api.student.dto.StudentRespDTO;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.TeacherApi;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.dto.TeacherRespDTO;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.vo.ClassesStudentListPageVO;
import com.nanhai.buddhist.college.module.study.controller.admin.attendancemanagement.vo.AttendanceManagementListReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.attendancemanagement.vo.AttendanceManagementRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.attendancemanagementlog.vo.AttendanceManagementLogPageReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.attendancemanagementlog.vo.AttendanceManagementLogRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.attendancemanagementlog.vo.ManagementLogResVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ClockRecordCreateReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ClockRecordPageReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ClockRecordRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ClockRecordTodayReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ClockRecordUserReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.timetable.vo.ClassTimetableRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.timetable.vo.ClassTimetableTeacherTodayVO;
import com.nanhai.buddhist.college.module.study.controller.student.attendance.vo.StudentManagementPageReqVO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.AttendanceManagementAddReq;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.AttendanceManagementCheckReqVO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.BatchAddClockRecordReqVO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.ManagementCheckPageReqVO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.StudentClockListReqVO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.SubjectStudentClockResVO;
import com.nanhai.buddhist.college.module.study.convert.attendancemanagement.AttendanceManagementConvert;
import com.nanhai.buddhist.college.module.study.convert.attendancemanagementlog.AttendanceManagementLogConvert;
import com.nanhai.buddhist.college.module.study.convert.classclockrecord.ClockRecordConvert;
import com.nanhai.buddhist.college.module.study.convert.timetable.ClassTimetableConvert;
import com.nanhai.buddhist.college.module.study.dal.dataobject.attendancemanagement.AttendanceManagementDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.attendancemanagementlog.AttendanceManagementLogDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.classclockrecord.ClockRecordDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.timetable.ClassTimetableDO;
import com.nanhai.buddhist.college.module.study.enums.AttendanceCheckStatusEnum;
import com.nanhai.buddhist.college.module.study.enums.ClockRoleTypeEnum;
import com.nanhai.buddhist.college.module.study.enums.ClockTypeEnum;
import com.nanhai.buddhist.college.module.study.service.attendancemanagement.AttendanceManagementService;
import com.nanhai.buddhist.college.module.study.service.attendancemanagementlog.AttendanceManagementLogService;
import com.nanhai.buddhist.college.module.study.service.classclockrecord.ClockRecordService;
import com.nanhai.buddhist.college.module.study.service.timetable.ClassTimetableService;
import com.nanhai.buddhist.college.module.system.api.user.AdminUserApi;
import com.nanhai.buddhist.college.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

/**
 * @author AKai
 * @date 2022/8/28
 */
@Api(tags = "教师移动端端 - 班级管理")
@RestController
@RequestMapping("/study/teacher/move")
@Validated
public class TeacherManageMoveController {

    @Resource
    private AttendanceManagementService attendanceManagementService;

    @Resource
    private ClockRecordService clockRecordService;


    @Resource
    private CollegeClassApi collegeClassApi;

    @Resource
    private StudentApi studentApi;

    @Resource
    private ClassTimetableService classTimetableService;

    @Resource
    private TeacherApi teacherApi;


    @Resource
    private AdminUserApi adminUserApi;




    @PostMapping("/apply/leave")
    @ApiOperation("请假申请")
    public CommonResult<String> createAttendanceManagement(@Valid @RequestBody AttendanceManagementAddReq createReqVO) {
        createReqVO.setUserType(ClockRoleTypeEnum.TEACHER.getRole());
        return success(attendanceManagementService.createAttendanceManagement(createReqVO));
    }


    @GetMapping("/list")
    @ApiOperation("获取个人请假列表")
    public CommonResult<List<AttendanceManagementRespVO>> getAttendanceManagementListByQue(@Validated AttendanceManagementListReqVO reqVO) {
        reqVO.setUserType(ClockRoleTypeEnum.TEACHER.getRole()+"");
        List<AttendanceManagementDO> list = attendanceManagementService.getAttendanceManagementListByQue(reqVO);
        return success(AttendanceManagementConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获取教师个人请假分页")
    public CommonResult<PageResult<AttendanceManagementRespVO>> getAttendanceManagementPage(@Valid StudentManagementPageReqVO pageVO) {
        PageResult<AttendanceManagementDO> pageResult = attendanceManagementService.getAttendanceManagementPage(pageVO);
        return success(AttendanceManagementConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/leave/check/page")
    @ApiOperation("获取审批请假分页")
    public CommonResult<PageResult<AttendanceManagementRespVO>> getAttendanceManagementCheckPage(@Valid ManagementCheckPageReqVO pageVO) {
        List<Integer> status = new ArrayList<Integer>();
        status.add(AttendanceCheckStatusEnum.PEND.getStatus());
        pageVO.setApprovalStatuses(status);
        PageResult<AttendanceManagementDO> pageResult = attendanceManagementService.getAttendanceManagementCheckPageApp(pageVO);
        return success(AttendanceManagementConvert.INSTANCE.convertPage(pageResult));
    }



    @GetMapping("/getTodayClock")
    @ApiOperation("获取今日课程打卡详情")
    public CommonResult<List<ClockRecordRespVO>> getTodayClock(@Validated ClockRecordTodayReqVO recordTodayReqVO) {
        List<ClockRecordDO> todayClock = clockRecordService.getTodayClock(recordTodayReqVO);
        return success(ClockRecordConvert.INSTANCE.convertList(todayClock));
    }



    @GetMapping("/classes/info")
    @ApiOperation("获取班级详情")
    public CommonResult<ClassesInfoDTO> getClassesInfoDTOById(@RequestParam("id") String id) {
        return collegeClassApi.getClassesInfoDTOById(id);
    }

    @GetMapping("/student/page")
    @ApiOperation("获取班级中学生列表")
    public CommonResult<PageResult<StudentRespDTO>> getClassesStudentList(@RequestParam ClassesStudentListPageVO req) {
        return studentApi.getStudentRespDTO(req);
    }


    @GetMapping("/student/clock/page")
    @ApiOperation("获取教师考勤列表--打卡记录")
    public CommonResult<PageResult<ClockRecordRespVO>> getClockRecordPage(@Valid ClockRecordPageReqVO pageVO) {
        pageVO.setRole(ClockRoleTypeEnum.TEACHER.getRole());
        PageResult<ClockRecordDO> pageResult = clockRecordService.getClockRecordPage(pageVO);
        return success(ClockRecordConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/byTeacher/List")
    @ApiOperation("根据教师id获取班级列表")
    public CommonResult<List<ClassesInfoDTO>> getClassesInfoDTOListByTeacher(@RequestParam("teacherId") String teacherId) {
        return collegeClassApi.getClassesInfoDTOListByTeacher(teacherId);
    }


    @GetMapping("/leave/get/info")
    @ApiOperation("获取请假详情")
    public CommonResult<AttendanceManagementRespVO> getAttendanceManagement(@RequestParam("id") String id) {
        AttendanceManagementDO attendanceManagement = attendanceManagementService.getAttendanceManagement(id);
        return success(AttendanceManagementConvert.INSTANCE.convert(attendanceManagement));
    }


    @PutMapping("/leave/check")
    @ApiOperation("请假审批")
    public CommonResult<Boolean> checkAttendanceLeave(@Valid @RequestBody AttendanceManagementCheckReqVO pageVO) {
        attendanceManagementService.checkAttendanceLeave(pageVO);
        return CommonResult.success(true);
    }

    @GetMapping("/getClassTimetableListByTeacherId")
    @ApiOperation("根据教师ID获得课程列表")
    public CommonResult<List<ClassTimetableRespVO>> getClassTimetableListByTeacherId(@RequestParam("teacherId") String teacherId) {
        List<ClassTimetableDO> list = classTimetableService.getClassTimetableListByTeacherId(teacherId);
        return success(classTimetableService.coverNames(list));
    }

    @PostMapping("/batchAddClockRecord")
    @ApiOperation("批量新增打卡记录")
    public CommonResult<Boolean> batchAddClockRecord(@Validated @RequestBody BatchAddClockRecordReqVO batchAddClockRecordReqVO) {
        clockRecordService.batchAddClockRecord( batchAddClockRecordReqVO);
        return success(true);
    }


    @PostMapping("/clock/create")
    @ApiOperation("创建打卡记录")
    public CommonResult<String> createClockRecord(@Valid @RequestBody ClockRecordCreateReqVO createReqVO) {
        createReqVO.setClockType(ClockTypeEnum.HAND.getType());
        createReqVO.setClockRole(ClockRoleTypeEnum.TEACHER.getRole());
        return success(clockRecordService.createClockRecord(createReqVO));
    }


    @GetMapping("/teacherAllList")
    @ApiOperation("获取所有教师列表")
    public CommonResult<List<TeacherRespDTO>> getAllTeacherList(){
        return teacherApi.getAllTeacherList();
    }



    @GetMapping("/log/page")
    @ApiOperation("获得请假审核记录分页")
    public CommonResult<PageResult<AttendanceManagementRespVO>> getAttendanceManagementLogPage(@Valid ManagementCheckPageReqVO pageVO) {

        if (ObjectUtil.isNull(pageVO.getApprovalStatuses())) {
            List<Integer> status = new ArrayList<Integer>();
            status.add(AttendanceCheckStatusEnum.NOT_PASS.getStatus());
            status.add(AttendanceCheckStatusEnum.PASS.getStatus());
            status.add(AttendanceCheckStatusEnum.REJECT.getStatus());
            pageVO.setApprovalStatuses(status);
        }
        PageResult<AttendanceManagementDO> pageResult = attendanceManagementService.getAttendanceManagementCheckPageApp(pageVO);
        return success(AttendanceManagementConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/getTeacherClock")
    @ApiOperation("获取教师课程打卡")
    public CommonResult<List<ClockRecordRespVO>> getTodayClockBySubjectId(@Validated ClockRecordUserReqVO reqVO) {
        List<ClockRecordDO> todayClock = clockRecordService.getTodayClockBySubjectId(reqVO);
        return success(ClockRecordConvert.INSTANCE.convertList(todayClock));
    }
    @GetMapping("/getTodayClassTimetable")
    @ApiOperation("教师获取今日课程列表")
    public CommonResult<List<ClassTimetableRespVO>> getTodayTeacherClassTimetableList(ClassTimetableTeacherTodayVO reqVo) {
        List<ClassTimetableDO> list = classTimetableService.getTodayTeacherClassTimetableList(reqVo);
        return success(classTimetableService.coverNames(list));
    }

    @GetMapping("/getClassTimetableInfo")
    @ApiOperation("获得课程")
    public CommonResult<ClassTimetableRespVO> getClassTimetable(@RequestParam("id") Long id) {
        ClassTimetableDO classTimetable = classTimetableService.getClassTimetable(id);
        List<ClassTimetableDO> list = new ArrayList<>(2);
        list.add(classTimetable);
        return success(classTimetableService.coverNames(list).get(0));
    }

    @GetMapping("/getQrCodeStr")
    @ApiOperation("获取生成二维码字符串")
    public CommonResult<String> getQrCodeStr(Long id) {
        return success(classTimetableService.getQrCodeStr(id));
    }



    @GetMapping("/getTeacherBossUser")
    @ApiOperation("获取教师上属用户信息")
    public CommonResult<List<AdminUserRespDTO>> getTeacherAdminUserId(@RequestParam String id) {
        final TeacherRespDTO data = teacherApi.getTeacherById(id).getData();
        if (ObjectUtil.isEmpty(data)) {
            return success(new ArrayList<AdminUserRespDTO>());
        }
        return adminUserApi.getTeacherCheckUserByUser(data.getCollegeId());
    }


    @GetMapping("/getStudentClockList")
    @ApiOperation("获取课程中学生的打卡详情 ")
    public CommonResult<List<SubjectStudentClockResVO>> getStudentClockList(@Valid StudentClockListReqVO reqVO) {
        return success(clockRecordService.getStudentClockList(reqVO));
    }




}
